package com.example.buildtable.controller;

import com.example.common.util.R;
import com.example.buildtable.pojo.MemberShiplevel;
import com.example.buildtable.service.MemberShiplevelService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@RestController
@Tag(name = "会员等级")
@RequestMapping(value = "/memberShiplevel/")
public class MemberShiplevelController {
    @Autowired
    private MemberShiplevelService memberShiplevelService;
    
    @Operation(summary = "查询所有会员等级数据")
    @GetMapping(value = "/getAllMemberShiplevel")
    public R getAllMemberShiplevel() {
        List<MemberShiplevel> memberShiplevelList = memberShiplevelService.list();
        return R.ok("查询所有会员等级数据成功").data("memberShiplevelList", memberShiplevelList);
    }
    
    @Operation(summary = "增加会员等级")
    @PostMapping(value = "/addMemberShiplevel")
    public R addMemberShiplevel(MemberShiplevel memberShiplevel) {
        boolean flag = memberShiplevelService.save(memberShiplevel);
        if (flag) {
            return R.ok("增加会员等级成功");
        } else {
            return R.ok("增加会员等级失败");
        }
    }

    @Operation(summary = "删除会员等级")
    @GetMapping(value = "/deleteMemberShiplevel/{memberShipLevelId}")
    public R deleteMemberShiplevel(@PathVariable("memberShipLevelId") Integer memberShipLevelId) {
        boolean flag = memberShiplevelService.removeById(memberShipLevelId);
        if (flag) {
            return R.ok("删除会员等级成功");
        } else {
            return R.ok("删除会员等级失败");
        }
    }

    @Operation(summary = "按照会员等级查看")
    @GetMapping(value = "/getMemberShiplevelById/{memberShipLevelId}")
    public R getMemberShiplevelById(@PathVariable("memberShipLevelId") Integer memberShipLevelId) {
        MemberShiplevel memberShiplevel = memberShiplevelService.getById(memberShipLevelId);
        if (memberShiplevel != null) {
            return R.ok("按照会员等级查看成功").data("memberShiplevel", memberShiplevel);
        } else {
            return R.error("按照会员等级查看失败");
        }
    }

    @Operation(summary = "修改会员等级")
    @PostMapping(value = "/updateMemberShiplevel")
    public R updateMemberShiplevel(MemberShiplevel memberShiplevel) {
        boolean flag = memberShiplevelService.updateById(memberShiplevel);
        if (flag) {
            return R.ok("修改会员等级成功");
        } else {
            return R.ok("修改会员等级失败");
        }
    }
}